/****************************************************************************
 Copyright (c) 2012-2012 lijiejun
 Copyright (c) 2010-2012 cocos2d-x.org
 Copyright (c) 2008-2010 Ricardo Quesada
 Copyright (c) 2011      Zynga Inc.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

package org.cocos2d.support
{
	import org.cocos2d.cocoa.CCPoint;
	import org.cocos2d.cocoa.CCSize;

	public class CCPointExtension
	{	
		public static const kCCPointEpsilon : Number =  ccMacros.FLT_EPSILON;
		
		public static function ccp(x : Number, y : Number) : CCPoint
		{
			return new CCPoint(x, y);
		}
		
		public static function ccpNeg(v : CCPoint) : CCPoint
		{
			return ccp(-v.x, -v.y);
		}
		
		public static function ccpAdd(v1 : CCPoint, v2 : CCPoint) : CCPoint
		{
			return ccp(v1.x + v2.x, v1.y + v2.y);
		}
		
		public static function ccpSub(v1 : CCPoint, v2 : CCPoint) : CCPoint
		{
			return ccp(v1.x - v2.x, v1.y - v2.y);
		}
		
		public static function ccpMult(v : CCPoint, s : Number) : CCPoint
		{
			return ccp(v.x * s, v.y * s);
		}
		
		public static function ccpMidPoint(v1 : CCPoint, v2 : CCPoint) : CCPoint
		{
			return ccpMult(ccpAdd(v1, v2), 0.5);
		}
		
		public static function ccpDot(v1 : CCPoint, v2 : CCPoint) : Number
		{
			return v1.x * v2.x + v1.y * v2.y;
		}
		
		public static function ccpCross(v1 : CCPoint, v2 : CCPoint) : Number
		{
			return v1.x * v2.y - v1.y * v2.x;
		}
		
		public static function ccpPerp(v : CCPoint) : CCPoint
		{
			return ccp(-v.y, v.x);
		}
		
		public static function ccpRPerp(v : CCPoint) : CCPoint
		{
			return ccp(v.y, -v.x);			
		}
		
		public static function ccpProject(v1 : CCPoint, v2 : CCPoint) : CCPoint
		{
			return ccpMult(v2, ccpDot(v1, v2) / ccpDot(v2, v2));
		}
		
		public static function ccpRotate(v1 : CCPoint, v2 : CCPoint) : CCPoint
		{
			return ccp(v1.x * v2.x - v1.y * v2.y, v1.x * v2.y + v1.y * v2.x);
		}
		
		public static function ccpUnrotate(v1 : CCPoint, v2 : CCPoint) : CCPoint
		{
			return ccp(v1.x * v2.x + v1.y * v2.y, v1.y * v2.x - v1.x * v2.y);
		}
		
		public static function ccpLengthSQ(v : CCPoint) : Number
		{
			return ccpDot(v, v);
		}
		
		public static function ccpDistanceSQ(p1 : CCPoint, p2 : CCPoint) : Number
		{
			return ccpLengthSQ(ccpSub(p1, p2));
		}
		
		public static function ccpLength(v : CCPoint) : Number
		{
			return Math.sqrt(ccpLengthSQ(v));
		}
		
		public static function ccpDistance(v1 : CCPoint, v2 : CCPoint) : Number
		{
			return ccpLength(ccpSub(v1, v2));
		}
		
		public static function ccpNormalize(v: CCPoint) : CCPoint
		{
			return ccpMult(v, 1.0 / ccpLength(v));
		}
		
		public static function ccpForAngle(a : Number) : CCPoint
		{
			return ccp(Math.cos(a), Math.sin(a));
		}
		
		public static function ccpToAngle(v : CCPoint) : Number
		{
			return Math.atan2(v.y, v.x);
		}
		
		public static function clampf(value : Number, min_inclusive : Number, max_inclusive : Number) : Number
		{
			if (min_inclusive > max_inclusive)
			{
				var ftmp : Number;
				ftmp = min_inclusive;
				min_inclusive = max_inclusive;
				max_inclusive = min_inclusive;
			}
			
			return value < min_inclusive ? min_inclusive : value < max_inclusive ? value : max_inclusive;
		}
		
		public static function ccpClamp(p : CCPoint, min_inclusive : CCPoint, max_inclusive : CCPoint) : CCPoint
		{
			return ccp(clampf(p.x, min_inclusive.x, max_inclusive.x), clampf(p.y, min_inclusive.y, max_inclusive.y));
		}
		
		public static function ccpFromSize(s : CCSize) : CCPoint
		{
			return ccp(s.width, s.height);
		}
		
		public static function ccpCompOp(p : CCPoint, del : Function) : CCPoint
		{
			return ccp(del(p.x), del(p.y));
		}
		
		public static function ccpLerp(a : CCPoint, b : CCPoint, alpha : Number) : CCPoint
		{
			return ccpAdd(ccpMult(a, 1.0 - alpha), ccpMult(b, alpha));
		}
		
		public static function ccpFuzzyEqual(a : CCPoint, b : CCPoint, variance : Number) : Boolean
		{
			if (a.x - variance <= b.x && b.x <= a.x + variance)
				if (a.y - variance <= b.y && b.y <= a.y + variance)
					return true;
			return false;
		}
		
		public static function ccpCompMult(a : CCPoint, b : CCPoint) : CCPoint
		{
			return ccp(a.x * b.x, a.y * b.y);
		}
		
		public static function ccpAngleSigned(a : CCPoint, b : CCPoint) : Number
		{
			var a2 : CCPoint = ccpNormalize(a);
			var b2 : CCPoint = ccpNormalize(b);
			var angle : Number = Math.atan2(a2.x * b2.y - a2.y * b2.x, ccpDot(a2, b2));
			if (Math.abs(angle) < CCPointExtension.kCCPointEpsilon) return 0.0;
			return angle;
		}
		
		public static function ccpAngle(a : CCPoint, b : CCPoint) : Number
		{
			var angle : Number = Math.acos(ccpDot(ccpNormalize(a), ccpNormalize(b)));
			if (Math.abs(angle) < CCPointExtension.kCCPointEpsilon) return 0.0;
			return angle;
		}
		
		public static function ccpRotateByAngle(v : CCPoint, pivot : CCPoint, angle : Number) : CCPoint
		{
			var r : CCPoint = ccpSub(v, pivot);
			var cosa : Number = Math.cos(angle), sina : Number = Math.sin(angle);
			var t : Number = r.x;
			r.x = t * cosa - r.y * sina + pivot.x;
			r.y = t * sina + r.y * cosa + pivot.y;
			return r;
		}
		
		// AS3 do not support passing parameters by ref, so here, I use CCPoint as the return value
		public static function ccpLineIntersect(A : CCPoint, B : CCPoint, 
								C : CCPoint, D : CCPoint,
								ST : CCPoint) : Boolean
		{
			if ((A.x == B.x && A.y == B.y) || (C.x == D.x && C.y == D.y))
			{
				return false;
			}
			
			const BAx : Number = B.x - A.x;
			const BAy : Number = B.y - A.y;
			const DCx : Number = D.x - C.x;
			const DCy : Number = D.y - C.y;
			const ACx : Number = A.x - C.x;
			const ACy : Number = A.y - C.y;
			
			const denom : Number = DCy * BAx - DCx * BAy;
			
			ST.x = DCx * ACy - DCy * ACx;
			ST.y = BAx * ACy - BAy * ACx;
			
			if (denom == 0)
			{
				if (ST.x == 0 && ST.y == 0)
				{
					return true;
				}
				
				return false;
			}
			
			ST.x = ST.x / denom;
			ST.y = ST.y / denom;
			
			return true;
		}
		
		public static function ccpSegmentIntersect(A : CCPoint, B : CCPoint, C : CCPoint, D : CCPoint) : Boolean
		{
			var ST : CCPoint = new CCPoint();
			
			if (ccpLineIntersect(A, B, C, D, ST)
				&& (ST.x >= 0.0 && ST.x <= 1.0 && ST.y >= 0.0 && ST.y <= 1.0))
				return true;
			
			return false;
		}
		
		public static function ccpIntersectPoint(A : CCPoint, B : CCPoint, C : CCPoint, D : CCPoint) : CCPoint
		{
			var ST : CCPoint = new CCPoint();
		
			if (ccpLineIntersect(A, B, C, D, ST))
			{
				var P : CCPoint = new CCPoint();
				P.x = A.x + ST.x * (B.x - A.x);
				P.y = A.y + ST.x * (B.y - A.y);
				return P;
			}
			
			return CCPoint.Zero;
		}
	}
}